//
//  CWMainTabbarController.swift
//  CWWeibo06
//
//  Created by ycw on 2017/7/21.
//  Copyright © 2017年 xiaowei. All rights reserved.
//

import UIKit

class CWMainTabbarController: UITabBarController,CWTabbarDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewController(childController: CWHomeController(), title: "首页", imageName: "tabbar_home")
        
        addChildViewController(childController: CWMassegeController(), title: "信息", imageName: "tabbar_message_center")
        
        addChildViewController(childController: CWFindController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(childController: CWMineController(), title: "我的", imageName: "tabbar_profile")
        
        let tabbar: CWTabbar = CWTabbar()
        
        tabbar.tabbarDelegate = self
        
        //如果闭包内部引用了self，composeCallBack被CWTabbar持有，而CWTabbar被self持有，会造成循环引用
        tabbar.composeCallBack = {[unowned self] in
            
            print("我是回调过来的闭包")
        }
        
        setValue(tabbar, forKey: "tabBar")
        
    }
    
    func CWtabbarDelegate() {
        
        print("我是代理")
    }

    func addChildViewController(childController: UIViewController,title: String,imageName: String) {
        
        childController.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
        
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(.alwaysOriginal)
        
        childController.title = title
        
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor(red: 253/255.0, green: 130/255.0, blue: 36/255.0, alpha: 1),NSFontAttributeName: UIFont.systemFont(ofSize: 14)], for: .selected)
        
        //使用了自定义的tabbar所以无效
        //tabBar.tintColor = UIColor(red: 253/255.0, green: 130/255.0, blue: 36/255.0, alpha: 1)
        
        let naviVC: CWNavigationController! = CWNavigationController(rootViewController: childController)
        
        addChildViewController(naviVC)
        
    }
    
    

}
